#include "stdafx.h"
#include "CommandParser.h"
#include "global.h"
/****************************************************************************************
 * Copyright (c) 2008~2009 All Rights Resverved by
 *  G-Net Integrated Service co. Ltd. 
 ****************************************************************************************/
/**
 * @file CommandParser.cpp
 * @brief 
 *
 * @author: zuji.pan
 *
 * @date 10/13/2009
 *
 * @version 0.2.0
 * @bug	 
 * @bug	 
 * @warning 
 * Revision History 
 * @if  CR/PR ID Author	   Date		  Major Change @endif
 *               zuji.pan 10/13/2009 create version 0.2.0\n
 ****************************************************************************************/
namespace parser
{

CommandParser::CommandParser(void)
{

}

CommandParser::~CommandParser(void)
{
	/** unregister all the command */
	std::map<UINT32,Command*>::iterator it  = m_commandList.begin();
	for (;it!= m_commandList.end();it++)
	{
		Command * p = it->second;
		if(p){
			delete p;
			p= NULL;
		}
	}
	m_commandList.clear();

}
void CommandParser::paserData( ISysBusObject* msg) {
	/**find the command*/
	assert(msg != NULL);
	if (!msg) return;
	UINT32 id = msg->get_ID();
	Command * cmd = findCommand(id);
	if (!cmd) return;
	/**call setMessage*/
	cmd->HandleMessage(msg);
}

Command * CommandParser::findCommand(UINT32 cmdID) const  {
	/**g_hash_table_lookup*/
	std::map<UINT32,Command*>::const_iterator it  = m_commandList.find(cmdID);
	if (it != m_commandList.end())
	{
		Command * p = it->second;
		if(p) return p;
	}
	/**return command*/
	return NULL;
}

void CommandParser::registerCommand(Command * cmd) {
 	assert(cmd);
	if(!cmd) return;
	/**find the command*/
	Command * c = findCommand(cmd->cmdID());
	/**if !=NULL,g_free it*/
	if(!c){
		m_commandList.insert(std::pair<UINT32,Command *>(cmd->cmdID(),cmd));
	}
}

}